ArcGIS API for Silverlight - Library Reference
ShareAsync(IEnumerable<String>,Nullable<Boolean>,Nullable<Boolean>,Action<IEnumerable<String>,Exception>) Method
See Also  Example Send comments on this topic
ESRI.ArcGIS.Client.Portal Namespace > ArcGISPortalItem Class > ShareAsync Method : ShareAsync(IEnumerable<String>,Nullable<Boolean>,Nullable<Boolean>,Action<IEnumerable<String>,Exception>) Method

groupIds
The IEnumerable (aka. a collection) of Group Ids (obtained via ArcGISPortalGroup.Id values) that you want to share the item with.
withEveryone
If true, this item will be shared with everyone, e.g., it will be publicly accessible. If explicitly set to false the item will be unshared from public.
withOrganization
If true, this item will be shared with the organization. If set to false, the item will be unshared from the organization.
callback
The callback executed when the result is available. The first argument of the callback is the IEnumerable (aka. a collection) of Groups to which the item could not be shared with.
Shares the ArcGISPortalItem with the specified list of ArcGISPortalGroup.Id values. This Method is available only to authenticated users.

Syntax

Visual Basic (Declaration) 
Public Overloads Sub ShareAsync( _
   ByVal groupIds As IEnumerable(Of String), _
   ByVal withEveryone As Nullable(Of Boolean), _
   ByVal withOrganization As Nullable(Of Boolean), _
   ByVal callback As Action(Of IEnumerable(Of String),Exception) _
) 

Remarks

In order to use the ArcGISPortalItem.ShareAsync Method, a user must be logged into the ArcGIS Online or ArcGIS Portal site with proper credentials. One way for a user to log into the portal site is via code using the IdentityManager Class.

The individual who is authenticated and logged into the portal site that desires to use the ArcGISPortalItem.ShareAsync Method to establish which Groups (via their ArcGISPortalGroup.Id values) the ArcGISPortalItem is part of MUST meet one of the following criteria:

  • The authenticated user is the owner of the ArcGISPortalItem.
  • The authenticated user is the Administrator of the Organization to which the ArcGISPortalItem belongs.
  • The ArcGISPortalItem has its PortalAccess value of Public and the authenticated user is the owner of the Group. Note: the ArcGISPortalItem may not have been created/owned by the authenticated user; it is only important that the ArcGISPortalItem be Public, no matter who created/owns the ArcGISPortalItem.
  • The ArcGISPortalItem has its PortalAccess value of Public and the authenticated user is the Administrator of the Organization to which Group belongs. Note: the ArcGISPortalItem may not have been created/owned by the authenticated user; it is only important that the ArcGISPortalItem be Public, no matter who created/owns the ArcGISPortalItem.

This version of ArcGISPortalItem.ShareAsync has the capability of adding Groups for the PortalAccess level of Shared AND also has the capability of changing the other PortalAccess levels at the same time. The following set of examples should help to clarify the using of this Method.

Assume that you had an IEnumerable collection of GroupID values called myGroupIDs. The ArcGISPortalItem object that will use this version of the ShareAsync Method is called myArcGISPortalItem. Furthermore, you have defined a callback function called CALLBACK_ArcGISPortalItem_ShareAsync that will handle what happens to the results of calling the ArcGISPortalIetm.ShareAsync Method. The following are several scenarios you could do:

Scenario 1: Add the PortalAccess level of Public AND set the GroupID values for the PortalAccess level of Shared:

C#:
myArcGISPortalItem.ShareAsync(myGroupIDs, true, false, CALLBACK_ArcGISPortalItem_ShareAsync);

VB.NET:
myArcGISPortalItem.ShareAsync(myGroupIDs, True, False, AddressOf CALLBACK_ArcGISPortalItem_ShareAsync)


Scenario 2: Add the PortalAccess level of Public AND not set any GroupID values for the PortalAccess level of Shared:

C#:
myArcGISPortalItem.ShareAsync(null, true, false, CALLBACK_ArcGISPortalItem_ShareAsync);

VB.NET:
myArcGISPortalItem.ShareAsync(Nothing, True, False, AddressOf CALLBACK_ArcGISPortalItem_ShareAsync)


Scenario 3: Add the PortalAccess level of Organization AND set the GroupID values for the PortalAccess level of Shared:

C#:
myArcGISPortalItem.ShareAsync(myGroupIDs, false, true, CALLBACK_ArcGISPortalItem_ShareAsync);

VB.NET:
myArcGISPortalItem.ShareAsync(myGroupIDs, False, True, AddressOf CALLBACK_ArcGISPortalItem_ShareAsync)


Scenario 4: Add the PortalAccess level of Organization AND not set any GroupID values for the PortalAccess level of Shared:

C#:
myArcGISPortalItem.ShareAsync(null, false, true, CALLBACK_ArcGISPortalItem_ShareAsync);

VB.NET:
myArcGISPortalItem.ShareAsync(Nothing, False, True, AddressOf CALLBACK_ArcGISPortalItem_ShareAsync)


Scenario 5: Add the PortalAccess level of Public and Organization AND set the GroupID values for the PortalAccess level of Shared:

C#:
myArcGISPortalItem.ShareAsync(myGroupIDs, true, true, CALLBACK_ArcGISPortalItem_ShareAsync);

VB.NET:
myArcGISPortalItem.ShareAsync(myGroupIDs, True, True, AddressOf CALLBACK_ArcGISPortalItem_ShareAsync)


Scenario 6: Add the PortalAccess level of Public and Organization AND not set any GroupID values for the PortalAccess level of Shared:

C#:
myArcGISPortalItem.ShareAsync(null, true, true, CALLBACK_ArcGISPortalItem_ShareAsync);

VB.NET:
myArcGISPortalItem.ShareAsync(Nothing, True, True, AddressOf CALLBACK_ArcGISPortalItem_ShareAsync)


Scenario 7: Add the PortalAccess level of Private AND set the GroupID values for the PortalAccess level of Shared:

C#:
myArcGISPortalItem.ShareAsync(myGroupIDs, false, false, CALLBACK_ArcGISPortalItem_ShareAsync);

VB.NET:
myArcGISPortalItem.ShareAsync(myGroupIDs, False, False, AddressOf CALLBACK_ArcGISPortalItem_ShareAsync)


Scenario 8: Add the PortalAccess level of Private AND not set any GroupID values for the PortalAccess level of Shared:

C#:
myArcGISPortalItem.ShareAsync(null, false, false, CALLBACK_ArcGISPortalItem_ShareAsync);

VB.NET:
myArcGISPortalItem.ShareAsync(Nothing, False, False, AddressOf CALLBACK_ArcGISPortalItem_ShareAsync)

NOTE: Setting the first parameter (groupIds) of the ArcGISPortalItem.ShareAsync to null/Nothing does not set any existing Group values. In other words, it does not empty the list of Groups associated with the ArcGISPortalItem. The way to remove existing Group values from the ArcGISPortalItem is to use the ArcGISPortalItem.UnshareAsync Method.

If it is desired to only change the Groups for the PortalAccess level of Shared and not bother changing the other PortalAccess levels for the ArcGISPortalItem, consider using the other version: ShareAsync(IEnumerable<String>,Action<IEnumerable<String>,Exception>)

Parameters

groupIds
The IEnumerable (aka. a collection) of Group Ids (obtained via ArcGISPortalGroup.Id values) that you want to share the item with.
withEveryone
If true, this item will be shared with everyone, e.g., it will be publicly accessible. If explicitly set to false the item will be unshared from public.
withOrganization
If true, this item will be shared with the organization. If set to false, the item will be unshared from the organization.
callback
The callback executed when the result is available. The first argument of the callback is the IEnumerable (aka. a collection) of Groups to which the item could not be shared with.

Example

How to use:

Edit the Member (i.e. Global) variables in the code-behind before running this sample. You need to supply Urls for an ArcGIS Online or ArcGIS Portal to get information about ArcGISPortalItems. It is required to supply the username and password for a user that can access the portal. - When the application loads, click on an ArcGISPortalItem in the ListBox, choose the various access level CheckBoxes and then click the button to update the ArcGISPortalItem. If the Shared CheckBox is chosen, you can also specify one or more Groups (via CheckBoxes) to associate with the ArcGISPortalItem. The results from the ArcGISPortalItem.ShareAsync will be displayed in the TextBox.

The XAML code in this example is used in conjunction with the code-behind (C# or VB.NET) to demonstrate the functionality.

The following screen shot corresponds to the code example in this page.

Viewing and changing PortalAccess on ArcGISPortalItems.

XAMLCopy Code
<Grid x:Name="LayoutRoot" Background="White">
    
  <!--
  Display the ArcGISPortalItems in a ListBox. The user can click on items in the ListBox and see information about 
  the ArcGISPortalItem in the other controls.
  -->
  <TextBlock Height="23" HorizontalAlignment="Left" Margin="2,124,0,0" Name="TextBlock1" Text="ArcGISPortalItems:" VerticalAlignment="Top" />
  <ListBox Height="455" HorizontalAlignment="Left" Margin="0,145,0,0" Name="ListBox_Results" VerticalAlignment="Top" Width="384"
           SelectionChanged="ListBox_Results_SelectionChanged">
    <ListBox.ItemTemplate>
      <DataTemplate>
        <StackPanel Orientation="Horizontal" >
          <StackPanel Orientation="Vertical"  >
            <TextBlock Text="{Binding Title, StringFormat='Title: {0}'}"/>
            <Image Source="{Binding ThumbnailUri}"  Stretch="None"  HorizontalAlignment="Left"/>
          </StackPanel>
        </StackPanel>
      </DataTemplate>
    </ListBox.ItemTemplate>
  </ListBox>
  <TextBox Height="318" HorizontalAlignment="Left" Margin="390,270,0,0" Name="TextBox1" VerticalAlignment="Top" Width="410" Background="Coral" />
  
  <!--
  Controls the user can manipulate to change the PortalAccess level and Groups associated with the selected ArcGISPortalItem.
  -->
  <CheckBox Content="Organization" Height="16" HorizontalAlignment="Left" Margin="470,101,0,0" Name="CheckBox_Organization" VerticalAlignment="Top" />
  <CheckBox Content="Public" Height="16" HorizontalAlignment="Left" Margin="390,101,0,0" Name="CheckBox_Public" VerticalAlignment="Top" />
  <CheckBox Content="Shared (aka. Groups)" Height="16" HorizontalAlignment="Left" Margin="390,125,0,0" Name="CheckBox_Shared" VerticalAlignment="Top" />
  <ListBox Height="90" HorizontalAlignment="Left" Margin="390,145,0,0" Name="ListBox_GroupsOwned" VerticalAlignment="Top" Width="410" >
    <ListBox.ItemTemplate>
      <DataTemplate>
        <StackPanel>
          <CheckBox IsChecked="{Binding Selected, Mode=TwoWay}" Content="{Binding Item.Title}" Tag="{Binding Item.Id}" />
        </StackPanel>
      </DataTemplate>
    </ListBox.ItemTemplate>
  </ListBox>
    
  <!-- Display the credential information for the user specified in the code-behind Member variables. -->
  <TextBlock Height="22" Margin="581,126,0,0" Name="TextBlock2" VerticalAlignment="Top" Text="Current User:" HorizontalAlignment="Left" Width="73" />
  <TextBlock Height="22" HorizontalAlignment="Left" Margin="660,125,0,0" Name="TextBlock_CurrentUser" VerticalAlignment="Top" Width="140" />
  
  <!-- Update the access level and Groups for the selected ArcGISPortalItem. -->
  <Button Content="ArcGISPortalItem.ShareAsync (ver1)" Height="23" HorizontalAlignment="Left" Margin="390,241,0,0" VerticalAlignment="Top" Width="410"
          Name="Button_ArcGISPortalItem_ShareAsync_Ver1"  Click="Button_ArcGISPortalItem_ShareAsync_Ver1_Click"/>
  
  <!-- Provide the instructions on how to use the sample code. -->
  <TextBlock Height="95" HorizontalAlignment="Left" Name="TextBlock_Instructions" VerticalAlignment="Top" Width="788" TextWrapping="Wrap" 
   Text="Edit the Member (i.e. Global) variables in the code-behind before running this sample. You need to supply Urls for an ArcGIS Online or 
   ArcGIS Portal to get information about ArcGISPortalItems. It is required to supply the username and password for a user that can access 
   the portal. - When the application loads, click on an ArcGISPortalItem in the ListBox, choose the various access level CheckBoxes and 
   then click the button to update the ArcGISPortalItem. If the Shared CheckBox is chosen, you can also specify one or more Groups (via 
   CheckBoxes) to associate with the ArcGISPortalItem. The results from the ArcGISPortalItem.ShareAsync will be displayed in the TextBox." />
  
</Grid>
C#Copy Code
public partial class MainPage : UserControl
{
  // ==========================================================================================
  // TODO: MODIFY THE FOLLOWING MEMBER (i.e. GLOBAL) VARIABLES THAT WORK FOR YOUR ORGANIZATION!
  
  // Specify the root location of ArcGIS Online or your ArcGIS Portal for testing.
  private const string _DefaultServerUrl = "http://www.YourArcGISPortal.com/sharing/rest";
  
  // Specify the location of ArcGIS Online or your ArcGIS Portal for generating a token for accessing secured services.
  private const string _DefaultTokenUrl = "https://www.YourArcGISPortal.com/sharing/generateToken";
  
  // Specify the UserName of the account in  ArcGIS Online or your ArcGIS Portal.
  private const string _Username = "YourUserName";
  
  // Specify the Password of the account in  ArcGIS Online or your ArcGIS Portal.
  private const string _Password = "YourPassword";
  // ==========================================================================================
  
  // Other Member (aka. Global) variables used in the application. (NO NEED TO MODIFY)
  private ESRI.ArcGIS.Client.Portal.ArcGISPortal _MyArcGISPortal = new ESRI.ArcGIS.Client.Portal.ArcGISPortal() { Url = _DefaultServerUrl };
  private ESRI.ArcGIS.Client.Portal.ArcGISPortalUser _MyArcGISPortalUser;
  private List<ArcGISPortalGroupExtended> _MyListOfArcGISPortalGroupExtended = new List<ArcGISPortalGroupExtended>();
  private ESRI.ArcGIS.Client.Portal.ArcGISPortalItem _MyArcGISPortalItem;
  
  public MainPage()
  {
    InitializeComponent();
    
    // Create a new instance of the IdentityManager.ServerInfos collection.
    System.Collections.Generic.List<ESRI.ArcGIS.Client.IdentityManager.ServerInfo> myServerInfos = null;
    myServerInfos = new System.Collections.Generic.List<ESRI.ArcGIS.Client.IdentityManager.ServerInfo>();
    
    // Create a new instance of a single IdentityManager.ServerInfo object. Using the user supplied Member variables defined
    // above set the ServerInfo.ServerUrl and ServerInfo.TokenServiceUrl Properties.
    ESRI.ArcGIS.Client.IdentityManager.ServerInfo myServerInfo = new ESRI.ArcGIS.Client.IdentityManager.ServerInfo();
    myServerInfo.ServerUrl = _DefaultServerUrl;
    myServerInfo.TokenServiceUrl = _DefaultTokenUrl;
    
    // Add the ServerInfo object to the ServerInfos collection.
    myServerInfos.Add(myServerInfo);
    
    // Determine the location of secure server and token endpoint.
    ESRI.ArcGIS.Client.IdentityManager.Current.RegisterServers(myServerInfos);
    
    // Define the function to challenge for credentials.
    ESRI.ArcGIS.Client.IdentityManager.Current.ChallengeMethod = CALLBACK_IdentityManager_ChallengeMethod;
    
    // Clear out any existing ArcGISPortalGroupExtended objects from the ListBox.
    ListBox_Results.ItemsSource = null;
    
    // Get the credential object that will be used to access secured token based services.
    ESRI.ArcGIS.Client.IdentityManager.Current.GenerateCredentialAsync(_DefaultServerUrl, _Username, _Password, CALLBACK_IdentityManager_GenerateCredentialAsync);
  }
  
  private void CALLBACK_IdentityManager_ChallengeMethod(string url, Action<ESRI.ArcGIS.Client.IdentityManager.Credential, Exception> callback, ESRI.ArcGIS.Client.IdentityManager.GenerateTokenOptions options)
  {
    // Activate the IdentityManager.
    callback(null, new NotImplementedException());
  }
  
  private void CALLBACK_IdentityManager_GenerateCredentialAsync(ESRI.ArcGIS.Client.IdentityManager.Credential crd, System.Exception ex)
  {
    // The callback that obtains the credential object.
    
    if (crd != null)
    {
      // We got a valid credential user token object.
      
      // Add the credential user token to the IdentityManager for accessing resources. 
      ESRI.ArcGIS.Client.IdentityManager.Current.AddCredential(crd);
      
      // Begin the process of accessing ArcGIS Online/ArcGIS Portal using the user specified Member variables defined above. 
      _MyArcGISPortal.InitializeAsync(_DefaultServerUrl, CALLBACK_ArcGISPortal_InitializeAsync);
    }
    else
    {
      // We had a problem generating the credentialed user token. Notify the user of the problem.
      MessageBox.Show("Could not log in. Please check credentials.");
    }
  }
  
  private void CALLBACK_ArcGISPortal_InitializeAsync(ESRI.ArcGIS.Client.Portal.ArcGISPortal credential, System.Exception exception)
  {
    // The callback that establishes valid communication with ArcGIS Online/ArcGIS Portal.
    
    if (credential != null && credential.CurrentUser != null)
    {
      // We have valid credentials.
      
      // Set the Member variable for the ArcGISPortalUser to the user credential that was found using the IdentityManager.
      _MyArcGISPortalUser = credential.CurrentUser;
      
      // Display the FullName of the credentialed user in the TextBlock.
      TextBlock_CurrentUser.Text = string.Format(credential.CurrentUser.FullName);
      
      // Clear out any existing ArcGISPortalGroupExtended objects from the ListBox.
      ListBox_Results.ItemsSource = null;
      
      // Set the ArcGISPortal.Url to the user defined Member variable.
      if (string.IsNullOrEmpty(_MyArcGISPortal.Url))
      {
        _MyArcGISPortal.Url = _DefaultServerUrl;
      }
      
      // Get the ArcGISPortalItems for the credentialed user.
      _MyArcGISPortalUser.GetItemsAsync(CALLBACK_ArcGISPortalUser_GetItemsAsync);
      
      // Get the Groups that an ArcGISPortalUser belongs to.
      _MyArcGISPortalUser.GetGroupsAsync(CALLBACK_ArcGISPortalUser_GetGroupsAsync);
    }
  }
  
  private void CALLBACK_ArcGISPortalUser_GetGroupsAsync(IEnumerable<ESRI.ArcGIS.Client.Portal.ArcGISPortalGroup> grps, System.Exception err)
  {
    // Add the Groups the credentialed user has to the ArcGISPortalGroupExtended collection.
    _MyListOfArcGISPortalGroupExtended.AddRange(
      from g in grps
      select new ArcGISPortalGroupExtended(g));
    
    // Add the ArcGISPortalGroupExtended objects to the ListBox.
    ListBox_GroupsOwned.ItemsSource = _MyListOfArcGISPortalGroupExtended;
  }
  
  private void CALLBACK_ArcGISPortalUser_GetItemsAsync(System.Collections.Generic.IEnumerable<ESRI.ArcGIS.Client.Portal.ArcGISPortalItem> userItems, System.Exception ex)
  {
    // Add the ArcGISPortalItems to the ListBox.
    ListBox_Results.ItemsSource = userItems;
  }
  
  private void Button_ArcGISPortalItem_ShareAsync_Ver1_Click(object sender, System.Windows.RoutedEventArgs e)
  {
    // Executes when the user clicks the Button. Changes the access level permissions for the selected ArcGISPortalItem.
    
    // Get the ArcGISPortalItem from the ListBox.
    _MyArcGISPortalItem = ListBox_Results.SelectedItem as ESRI.ArcGIS.Client.Portal.ArcGISPortalItem;
    
    // Warn the user that before they can change an access level on an ArcGISPortalItem, that they must first select it.
    if (_MyArcGISPortalItem == null)
    {
      MessageBox.Show("Select an ArcGISPortalItem from the ListBox before trying to change access level permissions.");
      return;
    }
    
    // Get the list of all the Groups the user can set for an ArcGISPortalItem.
    System.Collections.Generic.List<string> my_GroupIDs_ALL = new System.Collections.Generic.List<string>();
    foreach (ArcGISPortalGroupExtended myArcGISPortalGroupExtended in _MyListOfArcGISPortalGroupExtended)
    {
      ESRI.ArcGIS.Client.Portal.ArcGISPortalGroup myArcGISPortalGroup = myArcGISPortalGroupExtended.Item;
      my_GroupIDs_ALL.Add(myArcGISPortalGroup.Id);
    }
    
    // Get the list of selected Groups that have been check on by the user for setting Shared (i.e. Groups) items.
    System.Collections.Generic.List<string> mySelectedGroupIDs = new System.Collections.Generic.List<string>();
    foreach (ArcGISPortalGroupExtended myArcGISPortalGroupExtended in _MyListOfArcGISPortalGroupExtended)
    {
      if (myArcGISPortalGroupExtended.Selected)
      {
        ESRI.ArcGIS.Client.Portal.ArcGISPortalGroup myArcGISPortalGroup = myArcGISPortalGroupExtended.Item;
        mySelectedGroupIDs.Add(myArcGISPortalGroup.Id);
      }
    }
    
    // Do some extra work if the user wants to enable sharing with Groups.
    if (CheckBox_Shared.IsChecked == true)
    {
      // Make sure that at least one Group is chosen.
      if (mySelectedGroupIDs.Count == 0)
      {
        MessageBox.Show("At least one Group must be checked on  in order to set Shared access level permissions.");
        return;
      }
    }
    else
    {
      // No Groups selected.
      mySelectedGroupIDs = null;
    }
    
    // As a precaution first unshare all Grouped Items for the PortalAccess level of Shared. We will share them again if necessary in the
    // nested call to ArcGISPortalItem.ShareAsync.
    _MyArcGISPortalItem.UnshareAsync(my_GroupIDs_ALL, (result, err) =>
    {
      // Display any error messages if any.
      if (err != null)
      {
        MessageBox.Show(err.Message);
      }
      
      // Now set the PortalAccess level to be Shared for the Groups AND specify the other PortalAccess levels as well.
      // NOTE: if the parameters of withEveryone and withOrganization are both False, then the PortalAccess becomes Private!
      _MyArcGISPortalItem.ShareAsync(mySelectedGroupIDs, CheckBox_Public.IsChecked, CheckBox_Organization.IsChecked, CALLBACK_ArcGISPortalItem_ShareAsync);
    });
  }
  
  private void CALLBACK_ArcGISPortalItem_ShareAsync(System.Collections.Generic.IEnumerable<string> result, System.Exception err)
  {
    // This function executes when the changing of the ArcGISPortalItem access level changes.
    
    // Display an error messages.
    if (err != null)
    {
      MessageBox.Show(err.Message);
    }
    else
    {
      // Get the selected ArcGISPortalItem in the ListBox.
      _MyArcGISPortalItem = ListBox_Results.SelectedItem as ESRI.ArcGIS.Client.Portal.ArcGISPortalItem;
      
      // Make sure we have a valid ArcGISPortalItem. 
      if (_MyArcGISPortalItem == null)
      {
        return;
      }
      
      // Call the ArcGISPortalItem.GetSharingInfoAsync which will update the current access level information.
      _MyArcGISPortalItem.GetSharingInfoAsync(CALLBACK_ArcGISPortalItem_GetSharingInfoAsync);
    }
  }
  
  private void CALLBACK_ArcGISPortalItem_GetSharingInfoAsync(ESRI.ArcGIS.Client.Portal.ArcGISPortalSharingInfo sharinginfo, System.Exception ex)
  {
    // This function will display the access level and other information about the ArcGISPortalItem.
    
    // Display various information in the TextBox. Add more as you feel.
    System.Text.StringBuilder myStringBuilder = new System.Text.StringBuilder();
    
    ESRI.ArcGIS.Client.Portal.ArcGISPortalItem myPortalItem = sharinginfo.ArcGISPortalItem;
    myStringBuilder.Append("ArcGISPortalItem.Title = " + myPortalItem.Title + Environment.NewLine);
    myStringBuilder.Append("ArcGISPortalItem.AvgRating: " + myPortalItem.AvgRating.ToString() + Environment.NewLine);
    myStringBuilder.Append("ArcGISPortalItem.Type: " + myPortalItem.Type.ToString() + Environment.NewLine);
    myStringBuilder.Append("ArcGISPortalItem.Snippet: " + myPortalItem.Snippet + Environment.NewLine);
    myStringBuilder.Append("---------------------------------------------------" + Environment.NewLine);
    
    ESRI.ArcGIS.Client.Portal.PortalAccess myPortalAccess = sharinginfo.Access;
    myStringBuilder.Append("ArcGISPortalItem.PortalAccess = " + myPortalAccess.ToString() + Environment.NewLine);
    
    System.Collections.Generic.IEnumerable<string> myGroupIds = sharinginfo.GroupIds;
    foreach (string oneGroupId in myGroupIds)
    {
      myStringBuilder.Append("ArcGISPortalItem.GroupId = " + oneGroupId + Environment.NewLine);
    }
    
    TextBox1.Text = myStringBuilder.ToString();
  }
  
  private void ListBox_Results_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  {
    // Get the currently selected ArcGISPortalItem from the ListBox.
    _MyArcGISPortalItem = ListBox_Results.SelectedItem as ESRI.ArcGIS.Client.Portal.ArcGISPortalItem;
    
    // Make sure we have a valid ArcGISPortalItem. 
    if (_MyArcGISPortalItem == null)
    {
      return;
    }
    
    // Call the ArcGISPortalItem.GetSharingInfoAsync which will update the current access level information.
    _MyArcGISPortalItem.GetSharingInfoAsync(CALLBACK_ArcGISPortalItem_GetSharingInfoAsync);
  }
}
            
// A helper class to aid with setting up binding to the controls in XAML. It basically helps track the updating of the 
// ArcGISPortalGroup with the CheckBoxes.
public class ArcGISPortalGroupExtended : System.ComponentModel.INotifyPropertyChanged
{
  private bool selected_Renamed;
  public bool Selected
  {
    get
    {
      return selected_Renamed;
    }
    set
    {
      if (selected_Renamed != value)
      {
        selected_Renamed = value;
        OnPropertyChanged("Selected");
      }
    }
  }
  
  private ESRI.ArcGIS.Client.Portal.ArcGISPortalGroup item_Renamed;
  public ESRI.ArcGIS.Client.Portal.ArcGISPortalGroup Item
  {
    get
    {
      return item_Renamed;
    }
    set
    {
      if (item_Renamed != value)
      {
        item_Renamed = value;
        OnPropertyChanged("Item");
      }
    }
  }
  
  public ArcGISPortalGroupExtended(ESRI.ArcGIS.Client.Portal.ArcGISPortalGroup item)
  {
    this.Item = item;
  }
            
  public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
  private void OnPropertyChanged(string property)
  {
    if (PropertyChanged != null)
    PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(property));
  }
}
VB.NETCopy Code
Partial Public Class MainPage
  Inherits UserControl
  
  ' ==========================================================================================
  ' TODO: MODIFY THE FOLLOWING MEMBER (i.e. GLOBAL) VARIABLES THAT WORK FOR YOUR ORGANIZATION!
  
  ' Specify the root location of ArcGIS Online or your ArcGIS Portal for testing.
  Private Const _DefaultServerUrl As String = "http://www.YourArcGISPortal.com/sharing/rest"
  
  ' Specify the location of ArcGIS Online or your ArcGIS Portal for generating a token for accessing secured services.
  Private Const _DefaultTokenUrl As String = "https://www.YourArcGISPortal.com/sharing/generateToken"
  
  ' Specify the UserName of the account in  ArcGIS Online or your ArcGIS Portal.
  Private Const _Username As String = "YourUserName"
  
  ' Specify the Password of the account in  ArcGIS Online or your ArcGIS Portal.
  Private Const _Password As String = "YourPassword"
  ' ==========================================================================================
  
  ' Other Member (aka. Global) variables used in the application. (NO NEED TO MODIFY)
  Private _MyArcGISPortal As New ESRI.ArcGIS.Client.Portal.ArcGISPortal() With {.Url = _DefaultServerUrl}
  Private _MyArcGISPortalUser As ESRI.ArcGIS.Client.Portal.ArcGISPortalUser
  Private _MyListOfArcGISPortalGroupExtended As New List(Of ArcGISPortalGroupExtended)()
  Private _MyArcGISPortalItem As ESRI.ArcGIS.Client.Portal.ArcGISPortalItem
  
  Public Sub New()
    
    InitializeComponent()
    
    ' Create a new instance of the IdentityManager.ServerInfos collection.
    Dim myServerInfos As System.Collections.Generic.List(Of ESRI.ArcGIS.Client.IdentityManager.ServerInfo)
    myServerInfos = New System.Collections.Generic.List(Of ESRI.ArcGIS.Client.IdentityManager.ServerInfo)
    
    ' Create a new instance of a single IdentityManager.ServerInfo object. Using the user supplied Member variables defined
    ' above set the ServerInfo.ServerUrl and ServerInfo.TokenServiceUrl Properties.
    Dim myServerInfo As ESRI.ArcGIS.Client.IdentityManager.ServerInfo = New ESRI.ArcGIS.Client.IdentityManager.ServerInfo
    myServerInfo.ServerUrl = _DefaultServerUrl
    myServerInfo.TokenServiceUrl = _DefaultTokenUrl
    
    ' Add the ServerInfo object to the ServerInfos collection.
    myServerInfos.Add(myServerInfo)
    
    ' Determine the location of secure server and token endpoint.
    ESRI.ArcGIS.Client.IdentityManager.Current.RegisterServers(myServerInfos)
    
    ' Define the function to challenge for credentials.
    ESRI.ArcGIS.Client.IdentityManager.Current.ChallengeMethod = AddressOf CALLBACK_IdentityManager_ChallengeMethod
    
    ' Clear out any existing ArcGISPortalGroupExtended objects from the ListBox.
    ListBox_Results.ItemsSource = Nothing
    
    ' Get the credential object that will be used to access secured token based services.
    ESRI.ArcGIS.Client.IdentityManager.Current.GenerateCredentialAsync(_DefaultServerUrl, _Username, _Password, AddressOf CALLBACK_IdentityManager_GenerateCredentialAsync)
    
  End Sub
  
  Private Sub CALLBACK_IdentityManager_ChallengeMethod(ByVal url As String,
                          ByVal callback As Action(Of ESRI.ArcGIS.Client.IdentityManager.Credential, Exception),
                          ByVal options As ESRI.ArcGIS.Client.IdentityManager.GenerateTokenOptions)
    
    ' Activate the IdentityManager.
    callback(Nothing, New NotImplementedException())
    
  End Sub
  
  Private Sub CALLBACK_IdentityManager_GenerateCredentialAsync(crd As ESRI.ArcGIS.Client.IdentityManager.Credential, ex As System.Exception)
    
    ' The callback that obtains the credential object.
    
    If crd IsNot Nothing Then
      
      ' We got a valid credential user token object.
      
      ' Add the credential user token to the IdentityManager for accessing resources. 
      ESRI.ArcGIS.Client.IdentityManager.Current.AddCredential(crd)
      
      ' Begin the process of accessing ArcGIS Online/ArcGIS Portal using the user specified Member variables defined above. 
      _MyArcGISPortal.InitializeAsync(_DefaultServerUrl, AddressOf CALLBACK_ArcGISPortal_InitializeAsync)
      
    Else
      
      ' We had a problem generating the credentialed user token. Notify the user of the problem.
      MessageBox.Show("Could not log in. Please check credentials.")
      
    End If
    
  End Sub
  
  Private Sub CALLBACK_ArcGISPortal_InitializeAsync(credential As ESRI.ArcGIS.Client.Portal.ArcGISPortal, exception As System.Exception)
    
    ' The callback that establishes valid communication with ArcGIS Online/ArcGIS Portal.
    
    If credential IsNot Nothing AndAlso credential.CurrentUser IsNot Nothing Then
      
      ' We have valid credentials.
      
      ' Set the Member variable for the ArcGISPortalUser to the user credential that was found using the IdentityManager.
      _MyArcGISPortalUser = credential.CurrentUser
      
      ' Display the FullName of the credentialed user in the TextBlock.
      TextBlock_CurrentUser.Text = String.Format(credential.CurrentUser.FullName)
      
      ' Clear out any existing ArcGISPortalGroupExtended objects from the ListBox.
      ListBox_Results.ItemsSource = Nothing
      
      ' Set the ArcGISPortal.Url to the user defined Member variable.
      If String.IsNullOrEmpty(_MyArcGISPortal.Url) Then
        _MyArcGISPortal.Url = _DefaultServerUrl
      End If
      
      ' Get the ArcGISPortalItems for the credentialed user.
      _MyArcGISPortalUser.GetItemsAsync(AddressOf CALLBACK_ArcGISPortalUser_GetItemsAsync)
      
      ' Get the Groups that an ArcGISPortalUser belongs to.
      _MyArcGISPortalUser.GetGroupsAsync(AddressOf CALLBACK_ArcGISPortalUser_GetGroupsAsync)
      
    End If
    
  End Sub
  
  Private Sub CALLBACK_ArcGISPortalUser_GetGroupsAsync(grps As IEnumerable(Of ESRI.ArcGIS.Client.Portal.ArcGISPortalGroup), err As System.Exception)
    
    ' Add the Groups the credentialed user has to the ArcGISPortalGroupExtended collection.
    _MyListOfArcGISPortalGroupExtended.AddRange(
      From g In grps
      Select New ArcGISPortalGroupExtended(g))
    
    ' Add the ArcGISPortalGroupExtended objects to the ListBox.
    ListBox_GroupsOwned.ItemsSource = _MyListOfArcGISPortalGroupExtended
    
  End Sub
  
  Private Sub CALLBACK_ArcGISPortalUser_GetItemsAsync(userItems As System.Collections.Generic.IEnumerable(Of ESRI.ArcGIS.Client.Portal.ArcGISPortalItem), ex As System.Exception)
    
    ' Add the ArcGISPortalItems to the ListBox.
    ListBox_Results.ItemsSource = userItems
    
  End Sub
  
  Private Sub Button_ArcGISPortalItem_ShareAsync_Ver1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    
    ' Executes when the user clicks the Button. Changes the access level permissions for the selected ArcGISPortalItem.
    
    ' Get the ArcGISPortalItem from the ListBox.
    _MyArcGISPortalItem = TryCast(ListBox_Results.SelectedItem, ESRI.ArcGIS.Client.Portal.ArcGISPortalItem)
    
    ' Warn the user that before they can change an access level on an ArcGISPortalItem, that they must first select it.
    If _MyArcGISPortalItem Is Nothing Then
      MessageBox.Show("Select an ArcGISPortalItem from the ListBox before trying to change access level permissions.")
      Return
    End If
    
    ' Get the list of all the Groups the user can set for an ArcGISPortalItem.
    Dim my_GroupIDs_ALL As New System.Collections.Generic.List(Of String)
    For Each myArcGISPortalGroupExtended As ArcGISPortalGroupExtended In _MyListOfArcGISPortalGroupExtended
      Dim myArcGISPortalGroup As ESRI.ArcGIS.Client.Portal.ArcGISPortalGroup = myArcGISPortalGroupExtended.Item
      my_GroupIDs_ALL.Add(myArcGISPortalGroup.Id)
    Next
    
    ' Get the list of selected Groups that have been check on by the user for setting Shared (i.e. Groups) items.
    Dim mySelectedGroupIDs As New System.Collections.Generic.List(Of String)
    For Each myArcGISPortalGroupExtended As ArcGISPortalGroupExtended In _MyListOfArcGISPortalGroupExtended
      If (myArcGISPortalGroupExtended.Selected) Then
        Dim myArcGISPortalGroup As ESRI.ArcGIS.Client.Portal.ArcGISPortalGroup = myArcGISPortalGroupExtended.Item
        mySelectedGroupIDs.Add(myArcGISPortalGroup.Id)
      End If
    Next
    
    ' Do some extra work if the user wants to enable sharing with Groups.
    If CheckBox_Shared.IsChecked = True Then
    
      ' Make sure that at least one Group is chosen.
      If mySelectedGroupIDs.Count = 0 Then
        MessageBox.Show("At least one Group must be checked on  in order to set Shared access level permissions.")
        Return
      End If
      
    Else
    
      ' No Groups selected.
      mySelectedGroupIDs = Nothing
      
    End If
    
    ' As a precaution first unshare all Grouped Items for the PortalAccess level of Shared. We will share them again if necessary in the
    ' nested call to ArcGISPortalItem.ShareAsync.
    _MyArcGISPortalItem.UnshareAsync(my_GroupIDs_ALL, Sub(result, err)
                                                      
                                                      ' Display any error messages if any.
                                                      If err IsNot Nothing Then
                                                        MessageBox.Show(err.Message)
                                                      End If
                                                      
                                                      ' Now set the PortalAccess level to be Shared for the Groups AND specify the other PortalAccess levels as well.
                                                      ' NOTE: if the parameters of withEveryone and withOrganization are both False, then the PortalAccess becomes Private!
                                                      _MyArcGISPortalItem.ShareAsync(mySelectedGroupIDs, CheckBox_Public.IsChecked, CheckBox_Organization.IsChecked, AddressOf CALLBACK_ArcGISPortalItem_ShareAsync)
    
                                                      End Sub)
                                                        
  End Sub
  
  Private Sub CALLBACK_ArcGISPortalItem_ShareAsync(result As System.Collections.Generic.IEnumerable(Of String), err As System.Exception)
    
    ' This function executes when the changing of the ArcGISPortalItem access level changes.
    
    ' Display an error messages.
    If err IsNot Nothing Then
      MessageBox.Show(err.Message)
    Else
      
      ' Get the selected ArcGISPortalItem in the ListBox.
      _MyArcGISPortalItem = TryCast(ListBox_Results.SelectedItem, ESRI.ArcGIS.Client.Portal.ArcGISPortalItem)
      
      ' Make sure we have a valid ArcGISPortalItem. 
      If _MyArcGISPortalItem Is Nothing Then
        Return
      End If
      
      ' Call the ArcGISPortalItem.GetSharingInfoAsync which will update the current access level information.
      _MyArcGISPortalItem.GetSharingInfoAsync(AddressOf CALLBACK_ArcGISPortalItem_GetSharingInfoAsync)
    
    End If
    
  End Sub
  
  Private Sub CALLBACK_ArcGISPortalItem_GetSharingInfoAsync(sharinginfo As ESRI.ArcGIS.Client.Portal.ArcGISPortalSharingInfo, ex As System.Exception)
    
    ' This function will display the access level and other information about the ArcGISPortalItem.
    
    ' Display various information in the TextBox. Add more as you feel.
    Dim myStringBuilder As New Text.StringBuilder
    
    Dim myPortalItem As ESRI.ArcGIS.Client.Portal.ArcGISPortalItem = sharinginfo.ArcGISPortalItem
    myStringBuilder.Append("ArcGISPortalItem.Title = " + myPortalItem.Title + vbCrLf)
    myStringBuilder.Append("ArcGISPortalItem.AvgRating: " + myPortalItem.AvgRating.ToString + vbCrLf)
    myStringBuilder.Append("ArcGISPortalItem.Type: " + myPortalItem.Type.ToString + vbCrLf)
    myStringBuilder.Append("ArcGISPortalItem.Snippet: " + myPortalItem.Snippet + vbCrLf)
    myStringBuilder.Append("---------------------------------------------------" + vbCrLf)
    
    Dim myPortalAccess As ESRI.ArcGIS.Client.Portal.PortalAccess = sharinginfo.Access
    myStringBuilder.Append("ArcGISPortalItem.PortalAccess = " + myPortalAccess.ToString + vbCrLf)
    
    Dim myGroupIds As System.Collections.Generic.IEnumerable(Of String) = sharinginfo.GroupIds
    For Each oneGroupId As String In myGroupIds
      myStringBuilder.Append("ArcGISPortalItem.GroupId = " + oneGroupId + vbCrLf)
    Next
    
    TextBox1.Text = myStringBuilder.ToString
    
  End Sub
  
  Private Sub ListBox_Results_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
    
    ' Get the currently selected ArcGISPortalItem from the ListBox.
    _MyArcGISPortalItem = TryCast(ListBox_Results.SelectedItem, ESRI.ArcGIS.Client.Portal.ArcGISPortalItem)
    
    ' Make sure we have a valid ArcGISPortalItem. 
    If _MyArcGISPortalItem Is Nothing Then
      Return
    End If
    
    ' Call the ArcGISPortalItem.GetSharingInfoAsync which will update current access level information.
    _MyArcGISPortalItem.GetSharingInfoAsync(AddressOf CALLBACK_ArcGISPortalItem_GetSharingInfoAsync)
    
  End Sub
  
End Class
    
' A helper class to aid with setting up binding to the controls in XAML. It basically helps track the updating of the 
' ArcGISPortalGroup with the CheckBoxes.
Public Class ArcGISPortalGroupExtended
  Implements System.ComponentModel.INotifyPropertyChanged
  Private selected_Renamed As Boolean
  Public Property Selected() As Boolean
    Get
      Return selected_Renamed
    End Get
    Set(ByVal value As Boolean)
      If selected_Renamed <> value Then
        selected_Renamed = value
        OnPropertyChanged("Selected")
      End If
    End Set
  End Property
  
  Private item_Renamed As ESRI.ArcGIS.Client.Portal.ArcGISPortalGroup
  Public Property Item() As ESRI.ArcGIS.Client.Portal.ArcGISPortalGroup
    Get
      Return item_Renamed
    End Get
    Set(ByVal value As ESRI.ArcGIS.Client.Portal.ArcGISPortalGroup)
      If item_Renamed IsNot value Then
        item_Renamed = value
        OnPropertyChanged("Item")
      End If
    End Set
  End Property
            
  Public Sub New(ByVal item As ESRI.ArcGIS.Client.Portal.ArcGISPortalGroup)
    Me.Item = item
  End Sub
  
  Public Event PropertyChanged As System.ComponentModel.PropertyChangedEventHandler Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
  Private Sub OnPropertyChanged(ByVal [property] As String)
    RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs([property]))
  End Sub
  
End Class

Requirements

Target Platforms: Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family, Windows 7

See Also

© ESRI, Inc. All Rights Reserved.